#!/bin/bash 

installFile (){
#    set -x
    fromfile=$1 || (echo fromfile missing; return 1)
    tofile=$2   || (echo tofile missing; return 1)
    owner=$3 || ${JBOSSUS}
    group=$4 || ${JBOSSGRP}
    perms=$5 || 644
    
    if [ -f "${tofile}" ] ; then
        echo "found file in place, moved it" >> ${LogFile} 2>&1
        echo mv "${tofile}" "${tofile}.bak"  >> ${LogFile} 2>&1
        mv "${tofile}" "${tofile}.bak"       >> ${LogFile} 2>&1
    fi
    
    if [ -f "${fromfile}" ] ; then
        echo cp "${fromfile}" "${tofile}"   >> ${LogFile} 2>&1
        cp   "${fromfile}"      "${tofile}" >> ${LogFile} 2>&1
        chown ${owner}.${group} "${tofile}" >> ${LogFile} 2>&1
        chmod ${perms}          "${tofile}" >> ${LogFile} 2>&1
	return 0
    else
        echo "${fromfile} not found"        >> ${LogFile} 2>&1
        return 1
    fi
    

}





CopyConfig () {
#	set -x
    echo -n "Copy config/keystores from  ${NODE1_CONFDIR}"

    if [ ! -d ${NODE1_CONFDIR}/ejbca/conf ] ; then
	echo "${NODE1_CONFDIR}/ejbca/conf dont exist, you could put all ejbca config there then I would copy it into place" >> ${LogFile} 2>&1
    else
	cd ${NODE1_CONFDIR}/ejbca/conf
	for file in  *properties ; do
	    installFile ${file}  ${EJBCA_HOME}/conf/$file ${JBOSSUS} ${JBOSSGRP} 600        >> ${LogFile} 2>&1
	done
    fi

    if [ -f ${NODE1_CONFDIR}/ejbca.properties ] ; then
	    installFile ${NODE1_CONFDIR}/ejbca.properties ${EJBCA_HOME}/conf/ejbca.properties ${JBOSSUS} ${JBOSSGRP} 600 >> ${LogFile} 2>&1
    fi
    if [ -f ${NODE1_CONFDIR}/database.properties ] ; then
	    installFile ${NODE1_CONFDIR}/database.properties ${EJBCA_HOME}/conf/database.properties ${JBOSSUS} ${JBOSSGRP} 600 >> ${LogFile} 2>&1
    fi
    if [ -f ${NODE1_CONFDIR}/web.properties ] ; then
	    installFile ${NODE1_CONFDIR}/web.properties ${EJBCA_HOME}/conf/web.properties ${JBOSSUS} ${JBOSSGRP} 600 >> ${LogFile} 2>&1
    fi

    mkdir ${EJBCA_HOME}/p12                            >> ${LogFile} 2>&1
    mkdir ${JBOSS_HOME}/server/default/conf/keystore/  >> ${LogFile} 2>&1
    chmod 700 ${EJBCA_HOME}/p12                        >> ${LogFile} 2>&1
    chmod 700 ${JBOSS_HOME}/server/default/conf/keystore/                    >> ${LogFile} 2>&1
    chown ${JBOSSUS}.${JBOSSGRP} ${EJBCA_HOME}/p12                           >> ${LogFile} 2>&1
    chown ${JBOSSUS}.${JBOSSGRP} ${JBOSS_HOME}/server/default/conf/keystore/ >> ${LogFile} 2>&1

    if [ -f ${NODE1_CONFDIR}/truststore.jks ] ; then
	    installFile ${NODE1_CONFDIR}/truststore.jks ${EJBCA_HOME}/p12/truststore.jks ${JBOSSUS} ${JBOSSGRP} 600 >> ${LogFile} 2>&1
	    installFile ${NODE1_CONFDIR}/truststore.jks ${JBOSS_HOME}/server/default/conf/keystore/truststore.jks ${JBOSSUS} ${JBOSSGRP} 600 >> ${LogFile} 2>&1
    fi
    if [ -f ${NODE1_CONFDIR}/keystore.jks ] ; then
	    installFile ${NODE1_CONFDIR}/keystore.jks ${EJBCA_HOME}/p12/tomcat.jks ${JBOSSUS} ${JBOSSGRP} 600 >> ${LogFile} 2>&1
	    installFile ${NODE1_CONFDIR}/keystore.jks ${JBOSS_HOME}/server/default/conf/keystore/keystore.jks ${JBOSSUS} ${JBOSSGRP} 600 >> ${LogFile} 2>&1
    fi
    if [ -f ${NODE1_CONFDIR}/tomcat.jks ] ; then
	    installFile ${NODE1_CONFDIR}/tomcat.jks ${EJBCA_HOME}/p12/tomcat.jks ${JBOSSUS} ${JBOSSGRP} 600 >> ${LogFile} 2>&1
    fi

#    if [ -f ${NODE1_CONFDIR}/server.xml ] ; then
#	    installFile "${NODE1_CONFDIR}/server.xml" "${JBOSS_HOME}/server/default/deploy/jboss-web.deployer/server.xml" ${JBOSSUS} ${JBOSSGRP} 600 >> ${LogFile} 2>&1
#    fi
    
    echo "ok"
}

AddMysqlDriverToJboss(){
	echo -n "Adding Mysql Driver to JBoss: "	
	filenameMysqlDriver=$(basename ${MYSQLDRIVER})
	installFile ${MYSQLDRIVER} ${JBOSS_HOME}/server/default/lib/${filenameMysqlDriver} ${JBOSSUS} ${JBOSSGRP} 644          >> ${LogFile} 2>&1
	echo "ok"
	

}


StopJboss () {
    echo -n "Stoping jboss: "
    /etc/init.d/jboss stop	>> ${LogFile} 2>&1

    checkIfJbossIsRunning=`ps -ef | grep java | grep jboss | grep -v grep | awk '{print $2}' `
    if [ "x${checkIfJbossIsRunning}" != "x" ] ; then
	sleep 5
	kill -9 ${checkIfJbossIsRunning}
    fi       

    echo "ok"

}



#### ejbca bootstrap

EjbcaBootstrap () {
    echo -n "Running ejbca bootstrap: "
    cd ${EJBCA_HOME}
    
    su -p -s /bin/sh jboss -c "ant bootstrap" >> ${LogFile} 2>&1
    if [ $? -eq 0 ] ; then
	echo "ok"
    else
	echo
	echo "FAILED: ant bootstrap failed, see file: ${LogFile} for more info"
	exit 1
    fi
}


#### start jboss for the installing

StartJboss () {
    echo -n "Starting jboss: "
    /etc/init.d/jboss start	>> ${LogFile} 2>&1
    echo "ok"

}


######## ejbca upgrade ######
EjbcaUpgrade() {
    echo -n "Running ejbca upgrade: "
    
    cd ${EJBCA_HOME}

    su -p -s /bin/sh jboss -c "ant upgrade" 
    if [ $? -eq 0 ] ; then
	echo "Step ejbca upgrade: ok"
    else
	echo
	echo "FAILED: ejbca upgrade failed"
	exit 5
    fi
}

######## ejbca deploy ######
EjbcaDeploy() {
    echo -n "Running ejbca deploy: "
    
    cd ${EJBCA_HOME}

    su -p -s /bin/sh jboss -c "ant deploy" 
    if [ $? -eq 0 ] ; then
	echo "Step ejbca deploy: ok"
    else
	echo
	echo "FAILED: ejbca deploy failed"
	exit 5
    fi
}



ActivateCa(){
    
    echo "to activate CA:s run: ${SMARTCARD20_HOME}/activate-cas.sh"
    echo 
    exit 0
}


#"main"


LogFile="/tmp/smartcard2.0-ejbca-upgrade.$$"
NODE1_CONFDIR=/tmp/backup_htmf_conf
PATHENV=/etc/certificate-services.conf
if [ -r ${PATHENV} ] ; then
    source ${PATHENV}
else
    echo "${PATHENV} missing"
    exit
fi


echo "Welcome to $0, a tool to upgrade EJBCA"
echo "To view verbose install messages: tail -f ${LogFile}"

runningid=`id -u`
if [ "x${runningid}" != "x0" ] ; then
    echo "must run as root eg: sudo $0"
    exit
fi

echo "do you really want to continiue? (press enter)"
read a


StopJboss
CopyConfig
AddMysqlDriverToJboss

echo "Is this a master or slave node (M/s)?"
echo "If you select master host a ejbca database upgrade will occur"
echo -n ">"
read ms
if [ "x$ms" = "xs" -o "x$ms" = "xS" ] ; then
    echo "Bypassing EJBCA database upgrade step"
else
    EjbcaBootstrap
    StartJboss
    EjbcaUpgrade
    StopJboss
    sleep 10
fi

EjbcaDeploy
StartJboss

ActivateCa
